package com.zhq.blog.manage.domain.controller;

import javax.persistence.EntityManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;

import com.zhq.blog.core.domain.BossException;
import com.zhq.blog.core.domain.Page;
import com.zhq.blog.core.domain.admin.AdminControllerDescription;


public class BossModel extends ModelAndView{
	
	public static final String CONTROLLER_DESCRIPTION_FLAG = "controllerDescription";
	public static final String ACTION_NAME_FLAG = "actionName";
	public static final String ACTION_DESCRIPTION_FLAG = "actionDescription";
	
	protected Log logger = LogFactory.getLog(this.getClass());
	
	public BossModel(){
		
	}
	EntityManager entityManager;	
	
	AdminControllerDescription controllerDescription;
	
	String actionName;
	String actionDescription;
	
	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	public AdminControllerDescription getControllerDescription() {
		return controllerDescription;
	}


	public void setControllerDescription(
			AdminControllerDescription controllerDescription) {
		this.add(CONTROLLER_DESCRIPTION_FLAG, controllerDescription);
		this.controllerDescription = controllerDescription;
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.add(ACTION_NAME_FLAG, actionName);
		this.actionName = actionName;
	}

	public String getActionDescription() {		
		return actionDescription;
	}

	public void setActionDescription(String actionDescription) {
		this.add(ACTION_DESCRIPTION_FLAG, actionDescription);
		this.actionDescription = actionDescription;
	}
	public static final String VIEW_STATE_FLAG = "viewState";
	@SuppressWarnings("rawtypes")
	public BossModel add(String attributeName,Object attributeValue){
		if(attributeName != null && attributeValue!= null){ 
		if(attributeValue.getClass().getName() == org.springframework.data.domain.PageImpl.class.getName()){
			this.addObject(attributeName, new Page((org.springframework.data.domain.Page)attributeValue));
		}
		else{
			this.addObject(attributeName, attributeValue);
		}
		}else{
			if(!attributeName.equals(VIEW_STATE_FLAG) && !attributeName.equals(AdminLoginController.OPERATOR_FLAG)){
				logger.debug("attributeName or attributeValue is null when add it to BossModel");
				throw new BossException("您操作的资源不存在或者已删除。");		
			}
		}
		return this;
	}
	
}
